﻿#region Namespaces
using System;
using System.Collections;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Split.Core;
#endregion Namespaces

namespace Split
{
    public partial class SplitMainForm : Form
    {
        private static Regex isGuid = new Regex(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$");

        public SplitMainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Click event of the btnExecute control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnExecute_Click(object sender, EventArgs e)
        {
            this.m_statusLabel.Text = string.Empty;

            if (!IsGuid(txtCorrelationId.Text.Trim()))
            {
                this.m_statusLabel.Text = "Error: Correlation Id is not valid";
                return;
            }

            try
            {

                IList list = LogSearcher.GetLogFromCorrelation(txtCorrelationId.Text.Trim(), txtTimeSpan.Text.Trim());

                if (null == list ||
                    list.Count == 0)
                {
                    this.dgResult.DataSource = null;
                    this.m_statusLabel.Text = "There is no log available for this input. Check the correlation Id,Timespan and try again.";
                }
                else
                {
                    dgResult.DataSource = list;
                    dgResult.Columns.Remove("Correlation");
                    dgResult.Columns.Remove("EventId");
                    dgResult.Columns.Remove("Continuation");
                    dgResult.Columns.Remove("Context");
                    dgResult.Columns["Message"].Width = 200;
                    dgResult.AutoResizeColumns();
                    dgResult.ReadOnly = true;
                    dgResult.Update();
                }

            }
            catch (Exception ex)
            {
                this.m_statusLabel.Text = ex.Message;
            }
        }


        /// <summary>
        /// Determines whether the specified value is GUID.
        /// </summary>
        /// <param name="correlationID">The correlation ID.</param>
        /// <returns>
        /// 	<c>true</c> if the specified value is GUID; otherwise, <c>false</c>.
        /// </returns>
        private static bool IsGuid(string correlationID)
        {

            bool isValid = false;

            if (!string.IsNullOrEmpty(correlationID))
            {
                if (isGuid.IsMatch(correlationID))
                {
                    isValid = true;
                }
            }
            return isValid;
        }

        private void SplitMainForm_Load(object sender, EventArgs e)
        {
            this.txtCorrelationId.Focus();
        }

        void dgResult_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
        {
            e.SortResult = System.String.Compare(e.CellValue1.ToString(), e.CellValue2.ToString());
            e.Handled = true;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            this.txtCorrelationId.Text = string.Empty;
            this.txtTimeSpan.Text = string.Empty;
            this.m_statusLabel.Text = string.Empty;
        }

    }
}


